// pages/my-trainee/my-trainee.js

var app = getApp()
var req = require('../../utils/reqUtils.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    'class':{},
    'hidePopup':true,
    coaches:[],
    coachesT:[],
    selectCoach:'',
    selectCaId:0,
  },
 
  /**
   * 生命周期函数--监听页面加载 
   */
  onLoad(options) { 
    var that = this;
      req.post('/get_user_appointment_list/index',{'id':wx.getStorageSync('userInfo')['coach_id']},1).then(res=>{
        console.log(res);
        let calss = res.data.class_appointment_list;
        let locationSort = []
        locationSort = calss.sort((el1, el2) =>
          el1.start_date - el2.start_date
        ); 
        console.log(locationSort);
        calss.forEach(item=>{ 
          let year = new Date(item.start_date).getFullYear();
          let month= new Date(item.start_date).getMonth() +1;
          let day = new Date(item.start_date).getDate();
          item.date = year+'-'+ (month < 10 ? ('0' + month) : month)+'-'+(day < 10 ? ('0' + day) : day);
          let start_hours = new Date(item.start_date).getHours();
          let start_minutes= new Date(item.start_date).getMinutes();
          item.start_dates =(start_hours < 10 ? ('0' + start_hours) : start_hours)+':'+(start_minutes < 10 ? ('0' + start_minutes) : start_minutes);
          let end_hours = new Date(item.end_date).getHours();
          let end_minutes= new Date(item.end_date).getMinutes();
          item.end_dates =(end_hours < 10 ? ('0' + end_hours) : end_hours)+':'+(end_minutes < 10 ? ('0' + end_minutes) : end_minutes);
        })
        // let year = new Date(data.class_appointment_list.start_date).getFullYear();
        // let month= new Date(data.class_appointment_list.start_date).getMonth() +1;
        // let day = new Date(data.class_appointment_list.start_date).getDate();
        that.setData({
          class:res.data.class_appointment_list
        })
      })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  reserve:function(e){
    this.setData({ 
      hidePopup:false 
    })
    console.log(e);
    req.post('/coach/index', {}, 1).then(res => {
      console.log(res);
      let coachesT = [];
      for(var i=0;i<(res.data.coachList).length;i++){
        coachesT[i] = res.data.coachList[i].coach_name
      }
      this.setData({ 
        coaches:res.data.coachList,
        coachesT:coachesT,
        selectCaId:e.currentTarget.id
      })
    }); 
    // req.post('/transfer_coach/index',{})
  },
  setCoach:function(e){
    this.setData({ 
      selectCoach:this.data.coaches[e.detail.value],
      // coachesT:coachesT
    })
  },
  formSubmit:function(e){
    var that = this;
    console.log(this.data.selectCoach);
    console.log(this.data.selectCaId);
    req.post('/transfer_coach/index',{'ca_id':this.data.selectCaId,'coach_id':wx.getStorageSync('coachInfo')['coach_id'],'to_coach_id':this.data.selectCoach['coach_id']},1).then(res=>{
      console.log(res);
      this.setData({  
        hidePopup:true  
      })
    })
  },
  closePopupTap:function(){
    this.setData({
      hidePopup: true
    })
  }
}) 